#!/bin/bash

# 网络调试助手开发测试脚本

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

echo -e "${BLUE}🧪 启动网络调试助手开发服务${NC}"

# 项目目录
PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BACKEND_DIR="$PROJECT_DIR/backend"

# 检查Python依赖
echo -e "${YELLOW}📋 检查Python依赖...${NC}"
cd "$BACKEND_DIR"

if ! python3 -c "import fastapi" &> /dev/null; then
    echo -e "${YELLOW}📦 安装Python依赖...${NC}"
    pip3 install -r requirements.txt
fi

echo -e "${GREEN}✅ 依赖检查完成${NC}"

# 检查前端构建文件
if [ ! -d "$BACKEND_DIR/static" ]; then
    echo -e "${YELLOW}🏗️  前端未构建，正在构建...${NC}"
    cd "$PROJECT_DIR/front"
    npm run build
    echo -e "${GREEN}✅ 前端构建完成${NC}"
fi

# 启动后端服务
cd "$BACKEND_DIR"
echo -e "${BLUE}🚀 启动服务...${NC}"
echo -e "${GREEN}🌐 Web界面: http://localhost:10001${NC}"
echo -e "${GREEN}📚 API文档: http://localhost:10001/docs${NC}"
echo -e "${YELLOW}按 Ctrl+C 停止服务${NC}"
echo ""

python3 main.py